package com.loc;

import android.content.ContentResolver;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.TreeMap;

/* compiled from: WifiManagerWrapper.java */
/* loaded from: classes2.dex */
public final class fh {
    static long d;
    static long e;
    static long f;
    public static long g;
    static long h;
    public static HashMap<String, Long> v = new HashMap<>(36);
    public static long w = 0;
    static int x = 0;
    public static long z = 0;
    private es E;
    WifiManager a;
    Context i;
    ff t;
    ArrayList<eg> b = new ArrayList<>();
    ArrayList<eg> c = new ArrayList<>();
    boolean j = false;
    StringBuilder k = null;
    boolean l = true;
    boolean m = true;
    boolean n = true;
    private volatile fg B = null;
    String o = null;
    TreeMap<Integer, eg> p = null;
    public boolean q = true;
    public boolean r = true;
    public boolean s = false;
    private String C = "";
    long u = 0;
    ConnectivityManager y = null;
    private long D = 30000;
    volatile boolean A = false;

    public fh(Context context, WifiManager wifiManager, Handler handler) {
        this.a = wifiManager;
        this.i = context;
        ff ffVar = new ff(context, "wifiAgee", handler);
        this.t = ffVar;
        ffVar.a();
    }

    private void A() {
        try {
            if (gb.c(this.i, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19XSUZJX1NUQVRF")) {
                this.r = this.a.isWifiEnabled();
            }
        } catch (Throwable unused) {
            fv.b();
        }
    }

    private boolean B() {
        this.q = v();
        A();
        if (this.q && this.l) {
            if (f == 0) {
                return true;
            }
            if (gb.b() - f >= 4900 && gb.b() - g >= 1500) {
                int i = ((gb.b() - g) > 4900L ? 1 : ((gb.b() - g) == 4900L ? 0 : -1));
                return true;
            }
        }
        return false;
    }

    private static boolean a(int i) {
        int i2 = 20;
        try {
            i2 = WifiManager.calculateSignalLevel(i, 20);
        } catch (ArithmeticException e2) {
            ft.a(e2, "Aps", "wifiSigFine");
        }
        return i2 > 0;
    }

    public static long b() {
        return ((gb.b() - w) / 1000) + 1;
    }

    private void d(boolean z2) {
        String valueOf;
        ArrayList<eg> arrayList = this.b;
        if (arrayList == null || arrayList.isEmpty()) {
            return;
        }
        if (gb.b() - g > 3600000) {
            g();
        }
        if (this.p == null) {
            this.p = new TreeMap<>(Collections.reverseOrder());
        }
        this.p.clear();
        if (this.s && z2) {
            try {
                this.c.clear();
            } catch (Throwable unused) {
            }
        }
        int size = this.b.size();
        this.u = 0L;
        for (int i = 0; i < size; i++) {
            eg egVar = this.b.get(i);
            if (egVar.h) {
                this.u = egVar.f;
            }
            if (gb.a(egVar != null ? eg.a(egVar.a) : "") && (size <= 20 || a(egVar.c))) {
                if (this.s && z2) {
                    this.c.add(egVar);
                }
                if (!TextUtils.isEmpty(egVar.b)) {
                    valueOf = "<unknown ssid>".equals(egVar.b) ? "unkwn" : String.valueOf(i);
                    this.p.put(Integer.valueOf((egVar.c * 25) + i), egVar);
                }
                egVar.b = valueOf;
                this.p.put(Integer.valueOf((egVar.c * 25) + i), egVar);
            }
        }
        this.b.clear();
        for (eg egVar2 : this.p.values()) {
            this.b.add(egVar2);
        }
        this.p.clear();
    }

    public static String p() {
        return String.valueOf(gb.b() - g);
    }

    /* JADX WARN: Removed duplicated region for block: B:25:0x0087 A[Catch: all -> 0x0103, SecurityException -> 0x010e, TryCatch #2 {SecurityException -> 0x010e, all -> 0x0103, blocks: (B:4:0x0005, B:6:0x000f, B:8:0x0025, B:10:0x002b, B:12:0x0034, B:13:0x0038, B:15:0x003e, B:16:0x0050, B:18:0x0058, B:23:0x006e, B:25:0x0087, B:27:0x0091, B:29:0x0097, B:31:0x009f, B:33:0x00af, B:37:0x00ba, B:39:0x00dc, B:41:0x00ef, B:42:0x00f1, B:43:0x00fd, B:20:0x0060, B:21:0x0066, B:22:0x0069, B:7:0x0016), top: B:50:0x0005 }] */
    /* JADX WARN: Removed duplicated region for block: B:31:0x009f A[Catch: all -> 0x0103, SecurityException -> 0x010e, TryCatch #2 {SecurityException -> 0x010e, all -> 0x0103, blocks: (B:4:0x0005, B:6:0x000f, B:8:0x0025, B:10:0x002b, B:12:0x0034, B:13:0x0038, B:15:0x003e, B:16:0x0050, B:18:0x0058, B:23:0x006e, B:25:0x0087, B:27:0x0091, B:29:0x0097, B:31:0x009f, B:33:0x00af, B:37:0x00ba, B:39:0x00dc, B:41:0x00ef, B:42:0x00f1, B:43:0x00fd, B:20:0x0060, B:21:0x0066, B:22:0x0069, B:7:0x0016), top: B:50:0x0005 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private java.util.List<com.loc.eg> r() {
        /*
            Method dump skipped, instructions count: 278
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.loc.fh.r():java.util.List");
    }

    private int s() {
        WifiManager wifiManager = this.a;
        if (wifiManager != null) {
            return wifiManager.getWifiState();
        }
        return 4;
    }

    private boolean t() {
        long b = gb.b() - d;
        if (b < 4900) {
            return false;
        }
        if (!u() || b >= 9900) {
            if (x > 1) {
                long j = this.D;
                if (j == 30000) {
                    j = fs.o() != -1 ? fs.o() : 30000L;
                }
                if (Build.VERSION.SDK_INT >= 28 && b < j) {
                    return false;
                }
            }
            if (this.a != null) {
                d = gb.b();
                int i = x;
                if (i < 2) {
                    x = i + 1;
                }
                if (gb.c(this.i, "WYW5kcm9pZC5wZXJtaXNzaW9uLkNIQU5HRV9XSUZJX1NUQVRF")) {
                    return this.a.startScan();
                }
                ft.a(new Exception("n_cws"), "OPENSDK_WMW", "wfs_n_cws");
            }
            return false;
        }
        return false;
    }

    private boolean u() {
        if (this.y == null) {
            this.y = (ConnectivityManager) gb.a(this.i, "connectivity");
        }
        return a(this.y);
    }

    private boolean v() {
        if (this.a == null) {
            return false;
        }
        return gb.g(this.i);
    }

    private void w() {
        if (B()) {
            long b = gb.b();
            if (b - e >= 10000) {
                this.b.clear();
                h = g;
            }
            x();
            if (b - e >= 10000) {
                for (int i = 20; i > 0 && g == h; i--) {
                    try {
                        Thread.sleep(150L);
                    } catch (Throwable unused) {
                    }
                }
            }
        }
    }

    private void x() {
        if (B()) {
            try {
                if (t()) {
                    f = gb.b();
                }
            } catch (Throwable th) {
                ft.a(th, "WifiManager", "wifiScan");
            }
        }
    }

    private void y() {
        if (h != g) {
            List<eg> list = null;
            try {
                list = r();
            } catch (Throwable th) {
                ft.a(th, "WifiManager", "updateScanResult");
            }
            h = g;
            if (list == null) {
                this.b.clear();
                return;
            }
            this.b.clear();
            this.b.addAll(list);
        }
    }

    private void z() {
        try {
            if (this.a == null) {
                return;
            }
            int s = s();
            if (this.b == null) {
                this.b = new ArrayList<>();
            }
            if (s == 0 || s == 1 || s == 4) {
                g();
            }
        } catch (Throwable unused) {
        }
    }

    public final ArrayList<eg> a() {
        if (this.s) {
            b(true);
            return this.c;
        }
        return this.c;
    }

    public final void a(es esVar) {
        this.E = esVar;
    }

    public final void a(boolean z2) {
        Context context = this.i;
        if (!fs.n() || !this.n || this.a == null || context == null || !z2 || gb.c() <= 17) {
            return;
        }
        ContentResolver contentResolver = context.getContentResolver();
        try {
            if (((Integer) fx.a("android.provider.Settings$Global", "getInt", new Object[]{contentResolver, "wifi_scan_always_enabled"}, new Class[]{ContentResolver.class, String.class})).intValue() == 0) {
                fx.a("android.provider.Settings$Global", "putInt", new Object[]{contentResolver, "wifi_scan_always_enabled", 1}, new Class[]{ContentResolver.class, String.class, Integer.TYPE});
            }
        } catch (Throwable th) {
            ft.a(th, "WifiManagerWrapper", "enableWifiAlwaysScan");
        }
    }

    public final void a(boolean z2, boolean z3, boolean z4, long j) {
        this.l = z2;
        this.m = z3;
        this.n = z4;
        if (j < 10000) {
            this.D = 10000L;
        } else {
            this.D = j;
        }
    }

    public final boolean a(ConnectivityManager connectivityManager) {
        try {
            if (gb.a(connectivityManager.getActiveNetworkInfo()) == 1) {
                return a(m());
            }
            return false;
        } catch (Throwable th) {
            ft.a(th, "WifiManagerWrapper", "wifiAccess");
            return false;
        }
    }

    public final boolean a(fg fgVar) {
        if (fgVar == null) {
            return false;
        }
        boolean d2 = fgVar.d();
        if (!d2 && v()) {
            g();
        }
        return d2;
    }

    public final void b(boolean z2) {
        if (z2) {
            w();
        } else {
            x();
        }
        boolean z3 = false;
        if (this.A) {
            this.A = false;
            z();
        }
        y();
        if (gb.b() - g > 20000) {
            this.b.clear();
        }
        e = gb.b();
        if (this.b.isEmpty()) {
            g = gb.b();
            List<eg> r = r();
            if (r != null) {
                this.b.addAll(r);
                z3 = true;
            }
        }
        d(z3);
    }

    public final WifiInfo c() {
        try {
            if (this.a != null) {
                if (gb.c(this.i, "EYW5kcm9pZC5wZXJtaXNzaW9uLkFDQ0VTU19XSUZJX1NUQVRF")) {
                    return this.a.getConnectionInfo();
                }
                ft.a(new Exception("gci_n_aws"), "OPENSDK_WMW", "gci_n_aws");
                return null;
            }
            return null;
        } catch (Throwable th) {
            ft.a(th, "WifiManagerWrapper", "getConnectionInfo");
            return null;
        }
    }

    public final void c(boolean z2) {
        g();
        this.b.clear();
        this.t.a(z2);
    }

    public final String d() {
        return this.o;
    }

    public final ArrayList<eg> e() {
        if (this.b == null) {
            return null;
        }
        ArrayList<eg> arrayList = new ArrayList<>();
        if (!this.b.isEmpty()) {
            arrayList.addAll(this.b);
        }
        return arrayList;
    }

    public final void f() {
        try {
            this.s = true;
            List<eg> r = r();
            if (r != null) {
                this.b.clear();
                this.b.addAll(r);
            }
            d(true);
        } catch (Throwable unused) {
        }
    }

    public final void g() {
        this.B = null;
        this.b.clear();
    }

    public final void h() {
        z = System.currentTimeMillis();
        es esVar = this.E;
        if (esVar != null) {
            esVar.b();
        }
    }

    public final void i() {
        if (this.a != null && gb.b() - g > 4900) {
            g = gb.b();
        }
    }

    public final void j() {
        if (this.a == null) {
            return;
        }
        this.A = true;
    }

    public final boolean k() {
        return this.q;
    }

    public final boolean l() {
        return this.r;
    }

    public final fg m() {
        A();
        if (l()) {
            if (this.B == null) {
                Log.w("SystemApiWrapper", "getwifiAccess " + this.B);
                this.B = new fg(c());
            }
            return this.B;
        }
        return null;
    }

    public final boolean n() {
        return this.j;
    }

    public final String o() {
        boolean z2;
        String str;
        StringBuilder sb = this.k;
        if (sb == null) {
            this.k = new StringBuilder(700);
        } else {
            sb.delete(0, sb.length());
        }
        this.j = false;
        int size = this.b.size();
        int i = 0;
        boolean z3 = false;
        boolean z4 = false;
        while (i < size) {
            String a = eg.a(this.b.get(i).a);
            if (!this.m && !"<unknown ssid>".equals(this.b.get(i).b)) {
                z3 = true;
            }
            if (TextUtils.isEmpty(this.C) || !this.C.equals(a)) {
                z2 = z4;
                str = "nb";
            } else {
                str = "access";
                z2 = true;
            }
            this.k.append(String.format(Locale.US, "#%s,%s", a, str));
            i++;
            z4 = z2;
        }
        if (this.b.size() == 0) {
            z3 = true;
        }
        if (!this.m && !z3) {
            this.j = true;
        }
        if (!z4 && !TextUtils.isEmpty(this.C)) {
            StringBuilder sb2 = this.k;
            sb2.append("#");
            sb2.append(this.C);
            this.k.append(",access");
        }
        return this.k.toString();
    }

    public final long q() {
        return this.u;
    }
}
